home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / glchess / config.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.2 KB  |  282 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from defaults import *
  5.  
  6. try:
  7.     import gconf
  8.     import gobject
  9. except ImportError:
  10.     haveGConfSupport = False
  11.     _notifiers = { }
  12.     _values = { }
  13.     import xml.dom.minidom as xml
  14.     document = None
  15.     
  16.     try:
  17.         document = xml.dom.minidom.parse(CONFIG_FILE)
  18.     except IOError:
  19.         pass
  20.     except xml.parsers.expat.ExpatError:
  21.         print 'Configuration at ' + CONFIG_FILE + ' is invalid, ignoring'
  22.  
  23.     print 'Loading configuration from ' + CONFIG_FILE
  24.     
  25.     def _bool(string):
  26.         return string == 'True'
  27.  
  28.     valueTypes = {
  29.         'int': int,
  30.         'bool': _bool,
  31.         'float': float,
  32.         'str': str }
  33.     if document is not None:
  34.         elements = document.getElementsByTagName('config')
  35.         for e in elements:
  36.             for n in e.getElementsByTagName('value'):
  37.                 
  38.                 try:
  39.                     name = n.attributes['name'].nodeValue
  40.                 except KeyError:
  41.                     continue
  42.  
  43.                 
  44.                 try:
  45.                     valueType = n.attributes['type'].nodeValue
  46.                 except KeyError:
  47.                     continue
  48.  
  49.                 if len(n.childNodes) != 1 or n.childNodes[0].nodeType != n.TEXT_NODE:
  50.                     continue
  51.                 
  52.                 valueString = n.childNodes[0].nodeValue
  53.                 
  54.                 try:
  55.                     value = valueTypes[valueType](valueString)
  56.                 except KeyError:
  57.                     continue
  58.  
  59.                 _values[name] = value
  60.             
  61.         
  62.     
  63. except:
  64.     document is not None
  65.  
  66. haveGConfSupport = True
  67. _GCONF_DIR = '/apps/glchess/'
  68. _config = gconf.client_get_default()
  69.  
  70. try:
  71.     _config.add_dir(_GCONF_DIR[:-1], gconf.CLIENT_PRELOAD_NONE)
  72. except gobject.GError:
  73.     pass
  74.  
  75. _gconfGetFunction = {
  76.     gconf.VALUE_BOOL: gconf.Value.get_bool,
  77.     gconf.VALUE_FLOAT: gconf.Value.get_float,
  78.     gconf.VALUE_INT: gconf.Value.get_int,
  79.     gconf.VALUE_STRING: gconf.Value.get_string }
  80. _gconfSetFunction = {
  81.     bool: _config.set_bool,
  82.     float: _config.set_float,
  83.     int: _config.set_int,
  84.     str: _config.set_string,
  85.     unicode: _config.set_string }
  86. _defaults = {
  87.     'show_toolbar': True,
  88.     'show_history': True,
  89.     'maximised': False,
  90.     'fullscreen': False,
  91.     'show_3d': False,
  92.     'show_3d_smooth': False,
  93.     'show_move_hints': True,
  94.     'move_format': 'human',
  95.     'promotion_type': 'queen',
  96.     'board_view': 'human',
  97.     'show_comments': False,
  98.     'show_numbering': False,
  99.     'enable_networking': True,
  100.     'load_directory': '',
  101.     'save_directory': '',
  102.     'new_game_dialog/move_time': 0,
  103.     'new_game_dialog/white/type': '',
  104.     'new_game_dialog/white/difficulty': '',
  105.     'new_game_dialog/black/type': '',
  106.     'new_game_dialog/black/difficulty': '' }
  107.  
  108. class Error(Exception):
  109.     '''Exception for configuration use'''
  110.     pass
  111.  
  112.  
  113. def get(name):
  114.     """Get a configuration value.
  115.     
  116.     'name' is the name of the value to get (string).
  117.     
  118.     Raises an Error exception if the value does not exist.
  119.     """
  120.     if haveGConfSupport:
  121.         
  122.         try:
  123.             entry = _config.get(_GCONF_DIR + name)
  124.         except gobject.GError:
  125.             entry = None
  126.  
  127.         if entry is None:
  128.             
  129.             try:
  130.                 return _defaults[name]
  131.             except KeyError:
  132.                 raise Error('No config value: ' + repr(name))
  133.             except:
  134.                 None<EXCEPTION MATCH>KeyError
  135.             
  136.  
  137.         None<EXCEPTION MATCH>KeyError
  138.         
  139.         try:
  140.             function = _gconfGetFunction[entry.type]
  141.         except KeyError:
  142.             raise Error('Unknown value type')
  143.  
  144.         return function(entry)
  145.     
  146.     try:
  147.         return _values[name]
  148.     except KeyError:
  149.         haveGConfSupport
  150.         haveGConfSupport
  151.         
  152.         try:
  153.             return _defaults[name]
  154.         except KeyError:
  155.             raise Error('No config value: ' + repr(name))
  156.         except:
  157.             None<EXCEPTION MATCH>KeyError
  158.         
  159.  
  160.         None<EXCEPTION MATCH>KeyError
  161.  
  162.  
  163.  
  164. def set(name, value):
  165.     """Set a configuration value.
  166.     
  167.     'name' is the name of the value to set (string).
  168.     'value' is the value to set to (int, str, float, bool).
  169.     """
  170.     if haveGConfSupport:
  171.         
  172.         try:
  173.             function = _gconfSetFunction[type(value)]
  174.         except KeyError:
  175.             raise TypeError('Only config values of type: int, str, float, bool supported')
  176.  
  177.         
  178.         try:
  179.             function(_GCONF_DIR + name, value)
  180.         except gobject.GError:
  181.             pass
  182.         except:
  183.             None<EXCEPTION MATCH>gobject.GError
  184.         
  185.  
  186.     None<EXCEPTION MATCH>gobject.GError
  187.     
  188.     try:
  189.         oldValue = _values[name]
  190.     except KeyError:
  191.         pass
  192.  
  193.     if oldValue == value:
  194.         return None
  195.     _values[name] = value
  196.     
  197.     try:
  198.         watchers = _notifiers[name]
  199.     except KeyError:
  200.         oldValue == value
  201.         oldValue == value
  202.     except:
  203.         oldValue == value
  204.  
  205.     for func in watchers:
  206.         func(name, value)
  207.     
  208.     _save()
  209.  
  210.  
  211. def default(name):
  212.     set(name, _defaults[name])
  213.  
  214.  
  215. def _watch(client, _, entry, .3):
  216.     (function, name) = .3
  217.     value = get(name)
  218.     function(name, value)
  219.  
  220.  
  221. def watch(name, function):
  222.     '''
  223.     '''
  224.     if haveGConfSupport:
  225.         
  226.         try:
  227.             _config.notify_add(_GCONF_DIR + name, _watch, (function, name))
  228.         except gobject.GError:
  229.             pass
  230.         except:
  231.             None<EXCEPTION MATCH>gobject.GError
  232.         
  233.  
  234.     None<EXCEPTION MATCH>gobject.GError
  235.     
  236.     try:
  237.         watchers = _notifiers[name]
  238.     except KeyError:
  239.         watchers = _notifiers[name] = []
  240.  
  241.     watchers.append(function)
  242.  
  243.  
  244. def _save():
  245.     '''Save the current configuration'''
  246.     if haveGConfSupport:
  247.         return None
  248.     document = xml.dom.minidom.Document()
  249.     e = document.createComment('Automatically generated by glChess, do not edit!')
  250.     document.appendChild(e)
  251.     root = document.createElement('config')
  252.     document.appendChild(root)
  253.     valueNames = {
  254.         int: 'int',
  255.         bool: 'bool',
  256.         float: 'float',
  257.         str: 'str',
  258.         unicode: 'str' }
  259.     names = _values.keys()
  260.     names.sort()
  261.     for name in names:
  262.         value = _values[name]
  263.         e = document.createElement('value')
  264.         root.appendChild(e)
  265.         e.setAttribute('name', name)
  266.         e.setAttribute('type', valueNames[type(value)])
  267.         valueElement = document.createTextNode(str(value))
  268.         e.appendChild(valueElement)
  269.     
  270.     
  271.     try:
  272.         f = file(CONFIG_FILE, 'w')
  273.     except IOError:
  274.         haveGConfSupport
  275.         haveGConfSupport
  276.     except:
  277.         haveGConfSupport
  278.  
  279.     document.writexml(f)
  280.     f.close()
  281.  
  282.